<template>
    <div>
      <el-scrollbar height="600px">
      <el-card>
        <el-row :gutter="20" class="header">
            <el-col :span="6">
              <el-input placeholder="请输入作业名称"
              style="width: 100%"
              v-model="queryInfo.name" clearable @clear="getHomeworkList"></el-input>
            </el-col>
            <el-col :span="6">
              <el-input placeholder="请输入课程名称"
              style="width: 100%"
              v-model="queryInfo.courseName" clearable @clear="getHomeworkList"></el-input>
            </el-col>
            <el-col :span="6">
              <el-input placeholder="请输入课程序号"
              style="width: 100%"
              v-model="queryInfo.courseId" clearable @clear="getHomeworkList"></el-input>
            </el-col>
            <el-col :span="6">
              <el-input placeholder="请输入教师名称"
              style="width: 100%"
              v-model="queryInfo.teacherName" clearable @clear="getHomeworkList"></el-input>
            </el-col>
            <el-col :span="6" style="margin-top: 10px;">
              <el-button type="primary" :icon="Search" @click="getHomeworkList">搜索</el-button>
            </el-col>
          </el-row>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="index" width="50" />
          <el-table-column prop="courseId" label="课程序号" width="80" />
          <el-table-column prop="courseName" label="课程名" width="170" />
          <el-table-column prop="teacherName" label="教师名称" width="80" />
          <el-table-column prop="homeworkId" width="180" v-if="false"/>
          <el-table-column prop="homeworkName" label="作业名称" width="180" />
          <el-table-column prop="startTime" label="发布日期" width="180" />
          <el-table-column prop="endTime" label="截止日期" width="180"/>
          <el-table-column prop="type" width="180" v-if="false"/>
          <el-table-column label="查看详情">
            <template v-slot="scope">
              <el-button @click="viewDetails(scope.row)">详情</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background="true"
          layout="prev, pager, next"
          :total="total"
          v-model:currentPage="queryInfo.page"
          :page-size="queryInfo.size"
          @current-change="handlePageChange"
        />
      </el-card>
    </el-scrollbar>
    </div>
  </template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { Search } from '@element-plus/icons-vue'
import { getHomework } from '@/api/homeworks'

const router = useRouter()

const total = ref(0)

const currentPage = ref(1)

const queryInfo = ref({
  homeworkId: '',
  courseId: '',
  homeworkName: '',
  courseName: '',
  teacherName: '',
  page: currentPage,
  size: 5
})

const tableData = ref([])

const getHomeworkList = async () => {
  const res = await getHomework(queryInfo.value)
  console.log(res)
  tableData.value = res.data.data.rows
  total.value = res.data.data.total
}

const viewDetails = (row) => {
// 查看详情，这里应该是做页面跳转
  console.log('查看详情', row)
  if (row.type === '1') {
    router.push({ name: 'HomeworkDetailSC', params: { homeworkId: row.homeworkId } })
  } else {
    router.push({ name: 'HomeworkDetailS', params: { homeworkId: row.homeworkId } })
  }
}

const handlePageChange = () => {
  getHomeworkList()
}

onMounted(getHomeworkList)
</script>

<style>
.header{
  padding-bottom: 16px;
}
</style>
